//
//  PMineBusinessCardChangeBgViewController.m
//  Btpin
//
//  Created by Vic on 2019/12/21.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "PMineBusinessCardChangeBgViewController.h"
#import "BusinessCardChangeBgCell.h"

@interface PMineBusinessCardChangeBgViewController ()<UITableViewDelegate, UITableViewDataSource>
{
    NSMutableArray *itemDataArray;
    
    UITableView *cTableView;
}
@end

@implementation PMineBusinessCardChangeBgViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    itemDataArray = [NSMutableArray arrayWithCapacity:0];
    
    [self initUI];
    [self reloadData];
}
- (void)reloadData {
    SVPHUDShow;
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    
    [BaseNetManager requestWithMethod:POST path:Person_NameCardGetSkins_URL params:dic successBlock:^(NSDictionary * _Nonnull responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            NSString *idStr = [UIToolsControl dataAnalysis:responseObject[@"data"][@"skin_id"]];
            for (NSDictionary *dic in responseObject[@"data"][@"skins"]) {
                BusinessCardChangeBgModel *model = [[BusinessCardChangeBgModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                if ([idStr isEqualToString:model.id]) {
                    model.isSelect = YES;
                }
                [self->itemDataArray addObject:model];
            }
            [self->cTableView reloadData];
            SVPHUDDismiss;
        } else {
            SVPHUDError(responseObject[@"msg"]);
        }
    } failurBlock:^(NSError * _Nonnull error) {
        SVPHUDError(NetErrorAlertText);
        [self.noDataView vcNilStatusType:VCNilStatusTypeOfNoNet superView:self.view];
    }];
}
- (void)initUI {
    self.navigationItem.title = @"名片换肤";
    
    UIButton *okBtn = [UIToolsControl initButtonWithFrame:CGRectMake(0, 0, 44, 44) imageName:nil target:self action:@selector(okBtnTap) title:@"确定" titleColor:THEMECOLOR_OF_ORANGE];
    okBtn.titleLabel.font = Font(Font_15);
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:okBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    cTableView = [UIToolsControl initTableViewWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-HeightOfStatusBarAndNavigationBar-SIMEmojKeyBoardkBottomNOSafeArea_Height) target:self style:UITableViewStylePlain];
    cTableView.rowHeight = ScalePS(248);
    cTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    cTableView.backgroundColor = COLOR_OF_CLEAR;
    cTableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:cTableView];
}
- (void)okBtnTap {
    for (BusinessCardChangeBgModel *tempModel in itemDataArray) {
        if (tempModel.isSelect) {
            SVPHUDShow;
            NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
            dic = [UIToolsControl getPublicParameter];
            [dic setValue:tempModel.id forKey:@"skin_id"];
            
            [BaseNetManager requestWithMethod:POST path:Person_NameCardSetSkin_URL params:dic successBlock:^(NSDictionary * _Nonnull responseObject) {
                if ([responseObject[@"code"] integerValue] == 2000) {
                    
                    self.select(YES, tempModel.id, tempModel.image);
                    [self.navigationController popViewControllerAnimated:YES];
                    
                    SVPHUDDismiss;
                } else {
                    SVPHUDError(responseObject[@"msg"]);
                }
            } failurBlock:^(NSError * _Nonnull error) {
                SVPHUDError(NetErrorAlertText);
            }];
            break;
        }
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return itemDataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BusinessCardChangeBgCell *cell = [tableView dequeueReusableCellWithIdentifier:isCellID];
    if (!cell) {
        cell = [[BusinessCardChangeBgCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:isCellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    BusinessCardChangeBgModel *model = itemDataArray[indexPath.row];
    [cell reloadDataForModel:model];
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    for (BusinessCardChangeBgModel *tempModel in itemDataArray) {
        tempModel.isSelect = NO;
    }
    
    BusinessCardChangeBgModel *model = itemDataArray[indexPath.row];
    model.isSelect = YES;
    
    [tableView reloadData];
}

@end
